#!/usr/bin/env python
# -*- coding:utf-8 -*-
import datetime

import pymongo

try:
	mongo = pymongo.MongoClient()
	# 2. 创建库
	database = mongo['py3']
	# 3. 创建表
	collection = database['coll']
	# 4. 增加内容
	content = [
		{
			"name": 'bbb',
			'age': 26,
			'gender': False
		},
		{
			"name": 'ccc',
			'age': 32,
			'gender': False
		},
		{
			"name": 'ddd',
			'age': 90,
			'gender': True,
			'time':datetime.now()
		}
	]
	# collection.insert(content)	# 一个多个均可
	# collection.insert_one(content)
	# collection.insert_many(content)
	# 5. 删除内容
	# collection.delete_many({})  # 删除所有
	# collection.delete_one({'name': 'aaa'})
	# collection.delete_many({'gender': True})
	# 6. 修改内容
	data = collection.update_one({'age': 26}, {'$set': {'name':'BDb'}})
	print(data.matched_count)
	print(data.modified_count)
	# collection.update()
	# collection.update_many()
	# 7. 查询
	# data = collection.find_one({'name': 'BBB'})
	data = collection.find({'gender': False})
	for d in data:
		print(d)
	print(data)
except Exception as e:
	print(e)